﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace DotNet.WebUI.Admin
{
    public partial class DictEdit : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ViewState["Operate"] = GetQueryString("Operate", "New");
                ViewState["DictID"] = GetQueryString("DictID", "0");
                ViewState["Category"] = GetQueryString("Category", "0");

                if (int.Parse(ViewState["DictID"].ToString()) > 0)
                {
                    LoadDict();
                }
            }
        }

        private void LoadDict()
        {
            string sql = @"SELECT *
                FROM Dict
                WHERE DictID = {0} ";
            sql = string.Format(sql, ViewState["DictID"].ToString());

            DataRow objRow = ExecuteDataTable(sql).Rows[0];

            txtName.Text = objRow["Name"].ToString();
            txtDepict.Text = objRow["Depict"].ToString();
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {

                if (txtName.Text.Length <= 0)
                {
                    MessageBox("请输入名称！");
                    return;
                }

                string _Sql = "";
                string _Message = "";

                if (ViewState["Operate"].ToString().ToLower() == "new")
                {
                    _Sql = @"INSERT INTO Dict(Name, Depict, Category)
                        VALUES('{0}', '{1}', {2})";
                    _Sql = string.Format(_Sql,
                        txtName.Text,
                        txtDepict.Text,
                        ViewState["Category"].ToString());

                    _Message = "增加数据字典";
                }
                else if (ViewState["Operate"].ToString().ToLower() == "edit")
                {
                    _Sql = @"UPDATE Dict
                        SET Name = '{1}',
                            Depict = '{2}'
                        WHERE DictID = {0} ";

                    _Sql = string.Format(_Sql,
                        ViewState["DictID"].ToString(),
                        txtName.Text,
                        txtDepict.Text);

                    _Message = "修改数据字典";
                }

                if (ExecuteNonQuery(_Sql) >= 1)
                {
                    MessageBox("保存成功!");                    
                }
            }
            catch (Exception ex)
            {
                MessageBox(ex.Message);
            }
        }
    }
}